#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : bigDataProject 
@File    : yaml_file_read.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/11/10 11:25 
@Describe: yaml配置数据读取
"""
import os
from typing import Text, Any

import yaml

from config.setting import ensure_path_sep


class YamlFileRead:

    def __init__(self):
        _config_file = ensure_path_sep("\\config\\config.yaml")
        # 判断配置文件是否存在
        if os.path.exists(_config_file):
            # 读取配置文件
            with open(_config_file, 'r', encoding='utf-8') as yaml_file:
                yaml_file_reader = yaml_file.read()
                self._yaml_data = yaml.load(yaml_file_reader, yaml.FullLoader)
        else:
            raise FileNotFoundError("{}文件不存在~".format(_config_file))

    def read_yaml_data(self, config_item: Text) -> Any:
        """
        获取YAML文件配置信息
        :param config_item: 需要获取的键
        :return: 返回对应键下面的所有配置信息
        """
        _item_config_data = self._yaml_data[config_item]
        return _item_config_data


# 将各配置读取出来，放在变量里面，后续其他文件直接引用这些变量
config_data = YamlFileRead()
log_cfg = config_data.read_yaml_data('log')
mysql_info_cfg = config_data.read_yaml_data('mysqlInfo')
if __name__ == '__main__':
    print(mysql_info_cfg['member'])
